﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.BusinessEntities;
using Core;

namespace Utilities
{
    public enum InventoryDirection
    {
        In = 0,
        Out = 1
    }
    public enum InventoryType
    {
        OnHand = 0,
        SO = 1,
        PO = 2
    }
    public class Inventory
    {
        public static void UpdateInventory(InventoryDirection direct, InventoryType type,
                                            int iProductID,
                                            int iStockID,
                                            String strBatchNo,
                                            String strLotNo,
                                            String strSerialNo,
                                            double dQty
                                                )
        {

            ICProductsInfo Product = (ICProductsInfo)AppContext.ProductCtrl.GetObjectById(iProductID);
            if (Product == null) return;

            #region Calc InvStocks
            ICInvStocksInfo InvStock = AppContext.InvStockCtrl.GetObject(iProductID, iStockID);
            if (InvStock == null)
            {
                InvStock = new ICInvStocksInfo();
                InvStock.FK_ICProducts_ID = iProductID;
                InvStock.FK_ICStocks_ID = iStockID;
                
                InvStock.FK_ICStockUOMs_ID = Product.FK_ICStockUOMs_ID;
            }

            int iDirect = direct == InventoryDirection.In ? 1 : -1;
            if (type == InventoryType.OnHand)
                InvStock.OnHandQty += dQty * iDirect;
            else if (type == InventoryType.SO)
                InvStock.SOQty += dQty * iDirect;
            else if (type == InventoryType.PO)
                InvStock.POQty += dQty * iDirect;

            CalcInvStockInfo(InvStock);

            if (InvStock.ID > 0)
                AppContext.InvStockCtrl.Update(InvStock);
            else
                AppContext.InvStockCtrl.CreateObject(InvStock); 
            #endregion

            #region Calc InvStockItems
            if (type == InventoryType.OnHand)
            {
                ICInvStockItemsInfo InvStockItem = AppContext.InvStockItemCtrl.GetObject(iProductID, iStockID, strBatchNo, strLotNo, strSerialNo);
                if (InvStockItem == null)
                {
                    InvStockItem = new ICInvStockItemsInfo();
                    InvStockItem.FK_ICInvStocks_ID = InvStock.ID;
                    InvStockItem.FK_ICProducts_ID = iProductID;
                    InvStockItem.FK_ICStocks_ID = iStockID;
                    InvStockItem.BatchNo = strBatchNo;
                    InvStockItem.LotNo = strLotNo;
                    InvStockItem.SerialNo = strLotNo;
                }
                InvStockItem.Qty += iDirect * dQty;

                if (InvStockItem.ID == 0)
                    AppContext.InvStockItemCtrl.CreateObject(InvStockItem);
                else
                    AppContext.InvStockItemCtrl.Update(InvStockItem);
            } 
            #endregion

        }

        public static void CalcInvStockInfo(ICInvStocksInfo InvStock)
        {
            if (InvStock == null) return;

            InvStock.EstQty = Math.Round(InvStock.OnHandQty + InvStock.POQty - InvStock.SOQty, AppContext.CurrentCompanyInfo.NumberStockRound);
        }
    }
}
